<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,height=device-width initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>注册</title>
		<link rel="stylesheet" href="../../page/shuguang/css/login.css" />
	</head>
	<body>
		<div class="wrap">
			<div class="wrapTop">
				<img src="../../page/shuguang/img/bj.png"/>
			</div>
			<div class="loginMain">
				<div class="content">
					<div><label><span><img src="../../page/shuguang/img/user-icon.png" height="24"/></span><input type="text" name="" id="pName" value="" autocomplete="off" placeholder="输入用户名"/></label></div>
					<p class="tip" style="color:red;display: none;font-size: 14px;">用户名已占用</p>
					<div><label><span><img src="../../page/shuguang/img/password.png" height="24"/></span><input type="password" name="password" id="password" value=""  autocomplete="off" placeholder="输入密码"/></label></div>
					<div class="register"><p>注册</p></div>
					<div class="login_register"><span>已有账号，<a href="../shuguang/redirectToLogin.do">点击登录</a></span></div>
				</div>
			</div>
			<div class="wrapBottom">
				<img src="../../page/shuguang/img/round.png"/>
			</div>
		</div>
		<script src="../../page/shuguang/js/jquery.min.js"></script>
		<script src="../../page/shuguang/js/page.js"></script>
		<script>
			$(window).on("load",function(){
				$("#pName").val("");
				$("#password").val("");
				$("#pName").on("focus",function(){
					$(".tip").css({"display":"none"});
				});
				var flag;
				function checkUsername(onlyCheck){
					var pName = $("#pName").val();
					if(!pName){
						alert("用户名不能为空");
						return;
					}
					$.ajax({
						type: "post",
						url: "../shuguang/checkUsername.do",
						data:{"pName":pName},
						dataType: "json",
						success: function (data) {
							if (data.code == 201) {
								$("#pName").val("");
								$(".tip").css({"display":"block"});
								flag =  0;
							} else {
								$(".tip").css({"display":"none"});
								flag =  1;
							}
							if(flag && !onlyCheck){
								register();
							}
						}
					});
				}
				//注册
				function register() {
					var pName = $("#pName").val();
					var password = $("#password").val();
					if(!pName){
						alert("用户名不能为空");
						return;
					}else if(!password){
						alert("密码不能为空");
						return;
					}


					$.ajax({
						type: "post",
						url: "../shuguang/save.do",
						data:{"pName":pName,"password":password},
						dataType: "json",
						success: function (data) {
							if (data.code == 200) {
								window.location.href = "../shuguang/redirectToLogin.do";
							} else {

							}
						}
					});
				}



				$("#pName").on("blur",function(){
					checkUsername(true);
				});
				//注册页面
				$(".register").on("click",function(){
					checkUsername();
				});
			});
		</script>
	</body>
</html>
